package cn.szzsi._sys;

import cn.szzsi._Config;
import cn.szzsi._admin.BaseAdminController;
import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.SessionUtil;
import cn.szzsi.common.kit.WeixinMenuUtil;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.WeixinMenu;
import cn.szzsi.core.kit.StringUtils;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;

import java.util.List;
import java.util.UUID;

/**
 * Created by agfly on 2016/8/24.
 */
public class BusinessAppController extends Controller {
    private static final String LL = "kefu@@";
    private String shopId;
    /**
     * 微信自定义菜单
     */
    public void custommenu() {
        shopId= CommonUtil.PLATFROM_FLAG;
        List<WeixinMenu> menus = WeixinMenu.getByUid(shopId);
        String total_menu = "";
        for (WeixinMenu businessWeixinMenu : menus) {
            total_menu += "@" + businessWeixinMenu.getStr("id");
        }
        setAttr("total_menu", total_menu);
        setAttr("menu", "zdycd");
        setAttr("menus", menus);
        setAttr("apiid", _Config.p.get("wechat_apiid"));
        render("custommenu.html");

    }

    /**
     * 保存自定义菜单(保存到数据库,不调用微信接口)
     */
    public void savemenu() {
        shopId= CommonUtil.PLATFROM_FLAG;
        String total_menu = getPara("_total");//形式:@1@2@3@4
        //保存之前先将原来的菜单全部删除,已确保所有菜单都提交到后台
        if (StringUtils.isNotBlank(total_menu)) {//
            total_menu = total_menu.substring(1);
            String arr[] = total_menu.split("@");
            for (int i = 0; i < arr.length; i++) {
                Integer sortNo = getParaToInt("sortNo_" + arr[i] + "");//显示顺序
                String name = getPara("name_" + arr[i] + "");//主菜单名称
                String keyWord = getPara("keyWord_" + arr[i] + "");//触发关键词或链接地址
                Integer used = getParaToInt("used_" + arr[i] + "");//是否启用
                String parentId = getPara("parentId_" + arr[i] + "");//父级id
                //保存之前先判断是修改的还是新增的,如果是修改,则id不再改变,保存后原来的id
                if (WeixinMenu.dao.findById(arr[i]) != null) {
                    Db.update("delete from mall_weixin_menu where id=?", arr[i]);
                    WeixinMenu.saveMenu(arr[i], shopId, sortNo, name, keyWord, used, parentId);
                } else {
                    Db.update("delete from mall_weixin_menu where id=?", arr[i]);
                    WeixinMenu.saveMenu(UUID.randomUUID().toString(), shopId, sortNo, name, keyWord, used, parentId);
                }
            }
        }
        redirect("custommenu");

    }

    /**
     * 生成自定义菜单(调微信接口)
     */
    public  void createmenu() {
        String uid=CommonUtil.PLATFROM_FLAG;
        try {
            ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfigForB(this));
            String menu_str = WeixinMenuUtil.getMenu(uid);
            ApiResult apiResult = MenuApi.createMenu(menu_str);
            if(apiResult.isSucceed()){
                renderJson(Msg.SUCCESS);
            }else{
                System.out.println("错误----------------------"+apiResult.getErrorMsg());
                renderJson(Msg.fail(apiResult.getErrorMsg(),""));
            }
        }finally {
            ApiConfigKit.removeThreadLocalApiConfig();
        }


    }

    /**
     * 删除自定义菜单
     */
    public void deletemenu() {
        String id = getPara("id");
        WeixinMenu businessWeixinMenu = WeixinMenu.dao.findById(id);
        if ("0".equals(businessWeixinMenu.getStr("pid"))) {
            Db.update("delete from mall_weixin_menu where pid=?", id);
            businessWeixinMenu.delete();
        } else {
            businessWeixinMenu.delete();
        }
        businessWeixinMenu.delete();
        redirect("/mall/admin/businessapp/custommenu");
    }

    public static void main(String[] args) {
        System.out.println(StrKit.getRandomUUID());
    }





}





























